/**
 * Created by lwy on 2017/7/23.
 */

//个人收益
var page = 1;
var configName;
var id;
var name,
    value,
    memo;

//编辑
$('#save2').click(function(){
    $.ajax({
        url:'/crm/configC/edit.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            configName:$('#editName').val(),
            configValue:$('#editValue').val(),
            memo:$('#editMemo').val(),
            id:id,
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('编辑成功');
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})

function doAjax(){
    $.ajax({
        url:'/crm/userCouponC/search',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNo:page,
            configName:$("#configName").val(),
            type:'post'
        },
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.data.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas.data[i].id+'</td>'+
                        '<td class=" ">'+data.datas.data[i].configName+'</td>'+
                        '<td class=" ">'+data.datas.data[i].configValue+'</td>'+
                        '<td class=" ">'+data.datas.data[i].memo+'</td>'+
                        '<td class=" ">'+data.datas.data[i].userName+'</td>'+
                        '<td class="withdrawId"  data-toggle="modal" data-target="#myModal4" style="text-decoration: underline;cursor: pointer;" id="'+data.datas.data[i].id+'" name="'+data.datas.data[i].configName+'" value="'+data.datas.data[i].configValue+'" memo="'+data.datas.data[i].memo+'">编辑</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.nowPage,
                    turndown:'false',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
                //id处理
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    name = $(this).attr('name');
                    value = $(this).attr('value');
                    memo=$(this).attr('memo');
                    $('#editName').val(name);
                    $('#editValue').val(value);
                    $('#editMemo').val(memo)
                })
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    configName = $('#configName').val();
    page=1;
    doAjax();
})

//添加
$('#save').click(function(){
    $.ajax({
        url:'/crm/configC/add.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            configName:$('#addConfigName').val(),
            configValue:$('#addConfigValue').val(),
            memo:$('#addMemo').val()
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})